<?php

use yii\helpers\Html;
use frontend\modules\common\models\BaseParam;
use yii\widgets\Pjax;
use leandrogehlen\treegrid\TreeGrid;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $searchModel frontend\modules\resource\models\ResourceClassesSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '资源分类管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="resource-classes-index">

    <?php Pjax::begin(['id' => 'centerView']); ?>
    <div class="searchBody">
        <?= $this->render('_search', ['model' => $searchModel]); ?>
        <div class="addBtn">
            <?= Html::a('新增分类', 'javascript:;', ['class' => 'btn btn-success create']) ?>
        </div>
    </div>

    <?= TreeGrid::widget([
        'dataProvider' => $dataProvider,
        'keyColumnName' => 'id',
        'parentColumnName' => 'pid',
        'parentRootValue' => '0', //first parentId value
        'pluginOptions' => [
//            'initialState' => 'collapsed',
        ],
        'columns' => [
            'id',
            'pid' => [
                'attribute' => 'pid',
                'value' => function ($model) {
                    return $model->pid == 0 ? '一级分类' : '二级分类';
                }
            ],
            'title',
            'sort',
            'status' => [
                'attribute' => 'status',
                'value' => function ($model) {
                    return BaseParam::CodeHelper(BaseParam::$cateStatus, $model->status);
                }
            ],
            //功能
            [
                'class' => 'frontend\modules\common\models\ActionJsColumn',
                'template' => '{update}{cate-status}',
            ]
        ]
    ]); ?>

    <?php Pjax::end(); ?>
</div>
<!--增删改 操作-->
<?php echo $this->render('../../../common/views/base/_column', ['option' => ['u', 'c'],'height' => '360px']); ?>
<script>
    <?php $this->beginBlock('js_ok_remove') ?>
    //显示隐藏
    $(document).on('click', '.cate-status', function () {
        var id = $(this).closest('tr').data('key');

        $.ajax({
            type: "POST",
            url: '<?=Url::toRoute('cate-status')?>',
            data: {id: id},
            dataType: 'json'
        })
            .done(function (msg) {
                console.log(msg);
                if (msg['status'] == 200) {
                    layer.msg(msg['msg'], {icon: 1, time: 500}, function () {
                        $.pjax.reload({container: "#centerView"});
                    });
                } else {
                    layer.msg(msg['msg'], {icon: 2});
                }
            });
    });

    <?php $this->endBlock() ?>
</script>
<?php $this->registerJs($this->blocks['js_ok_remove'], \yii\web\View::POS_END); ?>
